package jsu.xxy.coffee3.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import jsu.xxy.coffee3.Utils.ExcelOutUtil;
import jsu.xxy.coffee3.Utils.ExcelUtils;
import jsu.xxy.coffee3.domain.Material;
import jsu.xxy.coffee3.domain.MaterialNoId;
import jsu.xxy.coffee3.mapper.MaterialMapper;
import jsu.xxy.coffee3.service.MaterialService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/material")
public class MaterialController {
    @Autowired
    MaterialMapper materialMapper;
    @Autowired
    MaterialService materialService;

    @PostMapping("/addMaterialExcel")
    @Operation(summary = "@RequestParam传一个文件 excel file")
    public boolean addMaterialExcel(@RequestParam MultipartFile file){
        List<MaterialNoId> materialList = ExcelUtils.excelToMaterial(file);
        for (int i = 0 ;i < materialList.size() ; i ++){
            materialService.insertMaterial(materialList.get(i));
        }
        return true;
    }


    @GetMapping("/getMaterialExcelMb")
    @Operation(summary = "导出excel模板，原材料信息 ,")
    public void getExcelMb(HttpServletResponse response){
        XSSFWorkbook wb = ExcelOutUtil.getDeviceDataByExcelMaterialMb();
        String fileName = "deviceData.xlsx";
        OutputStream outputStream = null;
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            //设置ContentType请求信息格式
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            outputStream = response.getOutputStream();
            wb.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/getMaterialExcel")
    @Operation(summary = "导出excel，原材料全部信息 ,")
    public void getExcel(HttpServletResponse response){
        XSSFWorkbook wb = ExcelOutUtil.getDeviceDataByExcelMaterial(materialMapper.selectList(null));

        String fileName = "deviceData.xlsx";
        OutputStream outputStream = null;
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            //设置ContentType请求信息格式
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            outputStream = response.getOutputStream();
            wb.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/getAll")
    @Operation(summary = "获得所有原材料，并且分页")
    public Page<Material> getAll(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize)
    {
        return materialService.getAll(pageNo,pageSize);

    }

    @PutMapping("/updateNum")
    @Operation(summary = "更新原材料数量")
    public Boolean upDataNum(@RequestBody Material material){
        return materialService.upDataNum(material);
    }
    @GetMapping("/getMaterial/{name}")
    @Operation(summary = "按名称查询原料，并且分页")
    public Page<Material> getMaterial(@PathVariable("name") String name, @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize)
    {
        return materialService.getMaterial(name,pageNo,pageSize);

    }

    @DeleteMapping("/deleteMaterial/{id}")
    @Operation(summary = "删除原材料")
    public Boolean deleteMaterial(@PathVariable("id")Integer id){
        return materialService.deleteMaterial(id);
    }

    @PostMapping("/addMaterial")
    @Operation(summary = "添加原材料")
    public Boolean insertMaterial(@RequestBody MaterialNoId materialNoId){
        return materialService.insertMaterial(materialNoId);

    }
    @DeleteMapping("/deleteMateritals/{ids}")
    public Boolean deleteStuffs(@PathVariable("ids") List<Integer> ids) {
        return materialService.deleteStuffs(ids);
    }


}
